<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/3/27
 * Time: 14:17
 * Desc: 商品原料库存
 */
namespace app\kuguan\model;
use think\Model;
use think\Db;
use PHPExcel;
use PHPExcel_IOFactory;

class MaterialStockModel extends Model{
    protected $pk = 'id';
    protected $table = 'ys_material_stock_log';
    protected $field = true;

    /**
     * @Author  lingyun
     * @Desc    导出原来库存日志数据
     * @param $arr  加收的参数
     * 2018-03-27
     */
    public function export_material_stock_log($arr){
        $map = [];
        $map['a.receive_time'] = ['neq',''];
        $map['b.is_deleted'] = ['eq',0];
        if(!empty($arr['id'])){
            $map['a.material_id'] = ['eq',$arr['id']];
        }
        if(!empty($arr['date'])){
            list($start, $end) = explode('-', str_replace(' ', '', $arr['date']));
            $start = $start."  00:00:00";		$end = $end." 23:59:59";
            $start = strtotime($start);			$end = strtotime($end);
            $map['a.receive_time'] = array(array('gt',$start),array('lt',$end),'AND');
        }

        //导出的数据
        $data = db('material_stock_log')->alias('a')->join('ys_goods_material b','a.material_id=b.id','LEFT')->where($map)->field('a.*,b.name,b.unit')->select();

        $filename = '原料库存记录'.date('YmdHis').'.xls';

        $objPHPExcel = new PHPExcel();//新建一个表
        $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
        $objSheet->setTitle("原料库存记录");

        //合并第一行单元格
//        $title = $order['order_sn'];
//        if($order['type'] == 1){
//            $title .= " - 购货";
//        }else{
//            $title .= " - 生产";
//        }
//        $title .= " - ".$order['price'];
//        if($order['receive_time'] == ''){
//            $title .= " - 未处理";
//        }else{
//            $title .= " - 已处理";
//        }
//
//        $objSheet->setCellValue('A1', $title);
//        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');      //合并
//        $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(     //加粗和居中
//            array(
//                'font' => array (
//                    'bold' => true
//                ),
////                'alignment' => array(
////                    'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
////                )
//            )
//        );

        $objSheet
            ->setCellValue('A1', '原料')
            ->setCellValue('B1', '单位')
            ->setCellValue('C1', '变更类型')
            ->setCellValue('D1', '变更数量')
            ->setCellValue('E1', '实际数量')
            ->setCellValue('F1', '差异数')
            ->setCellValue('G1', '当前库存')
            ->setCellValue('H1', '原料单价')
            ->setCellValue('I1', '原料总价')
            ->setCellValue('J1', '处理时间');


        $j = 2;     //从第二行开始

        foreach ($data as $key => $value) {
            if($value['sign'] == 1){        //进货
                $value['sign'] = '进货';
            }else if($value['sign'] == 2){  //生产
                $value['sign'] = '生产';
            }else{
                $value['sign'] = '报损';
            }

            $objSheet
                ->setCellValue('A' . $j, $value['name'])
                ->setCellValue('B' . $j, $value['unit'])
                ->setCellValue('C' . $j, $value['sign'])
                ->setCellValue('D' . $j, empty($value['del_num'])?0:$value['del_num'])
                ->setCellValue('E' . $j, empty($value['res_num'])?0:$value['res_num'])
                ->setCellValue('F' . $j, empty($value['dif_num'])?0:$value['dif_num'])
                ->setCellValue('G' . $j, $value['cur_num'])
                ->setCellValue('H' . $j, $value['signal_price'])
                ->setCellValue('I' . $j, $value['price'])
                ->setCellValue('J' . $j, date('Y-m-d H:i',$value['receive_time']));

            $j ++;
        }

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter->save("php://output");

    }
}